# -*- coding: utf-8 -*-
# @Time    : 2018/12/12 下午3:41
# @Author  : ZhiYuanYang
# @Email   : faryangsh@163.com
# @File    : Genericity.py
import logging
from logging import Logger
from typing import Sequence, TypeVar, Generic

T = TypeVar('T')  # Declare type variable

S = TypeVar('S', int)


def first(l: Sequence[T]) -> T:  # Generic function
    return l[0]


class LoggedVar(Generic[T]):
    def __init__(self, value: T, name: str, logger: Logger) -> None:
        self.name = name
        self.logger = logger
        self.value = value

    def set(self, new: T) -> None:
        self.log('Set ' + repr(self.value))
        self.value = new

    def get(self) -> T:
        self.log('Get ' + repr(self.value))
        return self.value

    def log(self, message: str) -> None:
        self.logger.info('%s: %s', self.name, message)


logging.basicConfig(format='%(message)s', level=logging.INFO)

if __name__ == '__main__':
    print(first(['34', '23']))

    log = LoggedVar(3, "tom", logging.getLogger('log'))
    log.set(4)

    S(1)
